//
//  GXPopoverAnimator.swift
//  weibopush
//
//  Created by 龚欣 on 15/6/27.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit

class GXPopoverAnimator: NSObject, UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {

    var isPresenting = false
    var presentFrame : CGRect = CGRectZero
    
    //返回负责转场的控制器
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let pop = PopOverPresentController(presentedViewController: presented, presentingViewController: presenting)
        pop.presentFrame = presentFrame
        
        return pop
    }
    
    //返回提供modal动画的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresenting = true
        return self
    }
    
    //返回提供dismiss动画的对象

    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresenting = false
        return self
    }
    
    
    //设置动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return isPresenting ? 1.3 : 0.25
    }
    
    //设置动画
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        if isPresenting{
            
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            transitionContext.containerView()?.addSubview(toView)
            
            toView.transform = CGAffineTransformMakeScale(1.0, 0)
            toView.layer.anchorPoint = CGPointMake(0.5, 0)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                toView.transform = CGAffineTransformIdentity
                }, completion: { (_) -> Void in
                    print("OK")
                    //关闭动画不要忘记
                    transitionContext.completeTransition(true)
            })
        }
        else{
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                //注意transform里面的参数为CGFloat，如果直接写0.0是没有动画效果的，但是如果是个非常小的数就有动画效果了
                fromView?.transform = CGAffineTransformMakeScale(1.0, 0.00001)
                }, completion: { (_) -> Void in
                    
                    fromView?.removeFromSuperview()
                    //关闭动画千万不要忘记
                    transitionContext.completeTransition(true)
            })
            
        }
        
    }

}
